package com.hs.biye.controller.admin.show;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hs.biye.mapper.DepartmentMapper;
import com.hs.biye.mapper.InhospitalMapper;
import com.hs.biye.mapper.PatientMapper;
import com.hs.biye.model.VO.DepartmentVo;
import com.hs.biye.model.entity.Department;
import com.hs.biye.model.entity.Inhospital;
import com.hs.biye.model.entity.Patient;
import com.hs.biye.model.reslut.Result;
import com.hs.biye.service.DepartmentService;
import com.hs.biye.service.InhospitalService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@RestController
@RequestMapping("/admin/data")
public class ShowDataController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private DepartmentMapper departmentMapper;


    @Resource
    private PatientMapper patientMapper;

    @Resource
    private InhospitalMapper inhospitalMapper;

    /**
     * 每月每部门住院信息
     * @return
     */
    @GetMapping("/b1")
    public Result<Map<String,List>>  data1(){
        HashMap<String,List> map = new HashMap<>();
        List<DepartmentVo> departmentNameAndId = departmentService.getDepartmentNameAndId();
        List departList = new ArrayList<>();
        List numberList = new ArrayList<>();
        departmentNameAndId.forEach(i->{
            departList.add(i.getDepartmentName());
        });

        LocalDate day = LocalDate.now();
        LocalDate bengin = day.with(TemporalAdjusters.firstDayOfMonth());

        departList.forEach(i->{
            String number = departmentMapper.selectCountOfDepartMonth(bengin,day,i.toString());
            if (number!=null && !number.equals("0") && !number.equals("") ){
                numberList.add(number);
            }else {
                numberList.add("0");
            }

        });
        map.put("d1",departList);
        map.put("d2",numberList);
        return Result.success(map);
    }


    /**
     * 近30天每日新增的用户
     * @return
     */
    @GetMapping("/b2")
    public Result<Map<String,List>> data2(){
        HashMap<String,List> map = new HashMap<>();
        List dateList = new ArrayList<>();
        List numberList = new ArrayList<>();
        LocalDate now = LocalDate.now();
        for (int i = 29 ; i > 0;i--){
            LocalDate localDate = now.minusDays(i);
            dateList.add(localDate);
        }
        dateList.add(now);

        for (Object date :dateList){
            LocalDateTime begin = LocalDateTime.of((LocalDate)date, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of((LocalDate)date, LocalTime.MAX);
            Long count = patientMapper.selectCount(new LambdaQueryWrapper<Patient>()
                    .ge(Patient::getCreateTime, begin).le(Patient::getCreateTime, end));
            numberList.add(count);
        }
        map.put("d1",dateList);
        map.put("d2",numberList);
        return Result.success(map);
    }




    //统计近30天的住院人数
    @GetMapping("/b3")
    public Result<Map<String,List>> data3(){
        HashMap<String,List> map = new HashMap<>();
        List dateList = new ArrayList<>();
        List numberList = new ArrayList<>();
        LocalDate now = LocalDate.now();
        for (int i = 29 ; i > 0;i--){
            LocalDate localDate = now.minusDays(i);
            dateList.add(localDate);
        }
        dateList.add(now);


        for (Object date :dateList){
            LocalDateTime begin = LocalDateTime.of((LocalDate)date, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of((LocalDate)date, LocalTime.MAX);
            Long count = inhospitalMapper.selectCount(new LambdaQueryWrapper<Inhospital>()
                    .isNull(Inhospital::getOutTime)
                    .ge(Inhospital::getCreateTime, begin).
                    le(Inhospital::getCreateTime, end));
            numberList.add(count);
        }
        map.put("d1",dateList);
        map.put("d2",numberList);
        return Result.success(map);
    }




    //近30添加的收入
    @GetMapping("/b4")
    public Result<Map<String,List>> data4(){
        HashMap<String,List> map = new HashMap<>();
        List dateList = new ArrayList<>();
        List numberList = new ArrayList<>();
        LocalDate now = LocalDate.now();
        for (int i = 29 ; i > 0;i--){
            LocalDate localDate = now.minusDays(i);
            dateList.add(localDate);
        }
        dateList.add(now);

        Random rand = new Random();
        for (Object date :dateList){
            LocalDateTime begin = LocalDateTime.of((LocalDate)date, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of((LocalDate)date, LocalTime.MAX);
            int i = rand.nextInt(10000) + 10000;
            BigDecimal count = new BigDecimal(String.valueOf(i));
            numberList.add(count.toString());
        }
        map.put("d1",dateList);
        map.put("d2",numberList);
        return Result.success(map);
    }
 }
